//
// Created by xqls on 12-8-28.
//
// To change the template use AppCode | Preferences | File Templates.
//


#import "CommnetMessageSendPanel.h"

#define CommnetMessageSendPanelHeight                44.0


@interface CommnetMessageSendPanel ()<XUIButtonDelegate>
{
    UIImageView *_entryImageView;
    
    UIImageView *_backgroundImageView;
    
    HPGrowingTextView *_textView;
    
    __weak id <CommnetMessageSendPanelDelegate> _delegate;
}

@end


@implementation CommnetMessageSendPanel

@synthesize delegate = _delegate;

@synthesize textView = _textView;

- (id)initWithFrame:(CGRect)frame
{
    frame.size.height = CommnetMessageSendPanelHeight;
    
    self = [super initWithFrame:frame];

    self.backgroundColor = [UIColor colorWithR:229 G:229 B:229 A:1];
 
    CGFloat leftPositon = 10;
    if([UIDevice is58InchScreen])
    {
        leftPositon = 25;
    }
    _textView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(leftPositon, 3, frame.size.width - leftPositon*2.0, 0)];//260 
    _textView.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);
    _textView.internalTextView.textInputMaxLength = 140;
    _textView.minNumberOfLines = 1;
    _textView.maxNumberOfLines = 3;
    _textView.returnKeyType = UIReturnKeyDefault; //just as an example
    _textView.font = [UIFont systemFontOfSize:15.0f];
    _textView.delegate = self;
    _textView.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(5, 0, 5, 0);
    _textView.returnKeyType = UIReturnKeySend;
    _textView.backgroundColor = [UIColor whiteColor];
    CGRect f = _textView.frame;
    f.origin.y = (self.height - f.size.height) / 2;
    _textView.frame = f;
    
    UIImage *entryBackground = [[UIImage imageNamed:@"input_input.png"] resizeableCenterImage];
    _entryImageView = [[UIImageView alloc] initWithImage:entryBackground];
    _entryImageView.frame = CGRectMake(_textView.left, _textView.top, _textView.width, _textView.height);
    
    UIImage *background = [[UIImage imageNamed:@"input_background.png"] resizeableCenterImage];
    _backgroundImageView = [[UIImageView alloc] initWithImage:background];
    _backgroundImageView.frame = CGRectMake(0, 0, self.width, frame.size.height);
    
    [self addSubview:_backgroundImageView];
    [self addSubview:_textView];
    [self addSubview:_entryImageView];
    
    return self;
}

-(void)dealloc
{
}

-(void)layoutSubviews
{
    [super layoutSubviews];
}

- (BOOL)canBecomeFirstResponder
{
    return [_textView canBecomeFirstResponder];
}

- (BOOL)becomeFirstResponder
{
    return [_textView becomeFirstResponder];
}

- (BOOL)canResignFirstResponder
{
    return [_textView canResignFirstResponder];
}

- (BOOL)resignFirstResponder
{
    return [_textView resignFirstResponder];
}

- (BOOL)isFirstResponder
{
    return [_textView isFirstResponder];
}

#pragma mark --
#pragma mark -- textInput

- (BOOL)growingTextViewShouldBeginEditing:(HPGrowingTextView *)growingTextView
{
    if ([_delegate respondsToSelector:@selector(commnetMessageSendPanelShouldBeginEdit:)] == YES)
        return [_delegate commnetMessageSendPanelShouldBeginEdit:self];

    return YES;
}

- (BOOL)growingTextViewShouldEndEditing:(HPGrowingTextView *)growingTextView
{
    if ([_delegate respondsToSelector:@selector(commnetMessageSendPanelShouldEndEdit:)] == YES)
        return [_delegate commnetMessageSendPanelShouldEndEdit:self];

    return YES;
}

- (void)growingTextViewDidBeginEditing:(HPGrowingTextView *)growingTextView
{
    if ([_delegate respondsToSelector:@selector(commnetMessageSendPanelDidBeginEdit:)] == YES)
        [_delegate commnetMessageSendPanelDidBeginEdit:self];
}

- (void)growingTextViewDidEndEditing:(HPGrowingTextView *)growingTextView
{
    if ([_delegate respondsToSelector:@selector(commnetMessageSendPanelDidEndEdit:)] == YES)
        [_delegate commnetMessageSendPanelDidEndEdit:self];
}

- (BOOL)growingTextView:(HPGrowingTextView *)growingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//    if([_delegate respondsToSelector:@selector(commnetMessageSendPanel:shouldChangeTextInRange:replacementText:)])
//        return [_delegate commnetMessageSendPanel:self shouldChangeTextInRange:range replacementText:text];
    
    return YES;
}

- (void)growingTextViewDidChange:(HPGrowingTextView *)growingTextView
{
    if ([_delegate respondsToSelector:@selector(commnetMessageSendPanelTextDidChanged:)] == YES)
        [_delegate commnetMessageSendPanelTextDidChanged:self];
}

- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(CGFloat)height
{
    float diff = (height - _textView.height);
    CGRect r = self.frame;
    r.size.height += diff;
    r.origin.y -= diff;
    self.frame = r;
    
    _entryImageView.height += diff;
    _backgroundImageView.height += diff;
    _textView.height += diff;

//    if ([_delegate respondsToSelector:@selector(commnetMessageSendPanel:inputPanelHeightWillChanged:)] == YES)
//        [_delegate commnetMessageSendPanel:self inputPanelHeightWillChanged:diff];
}

- (void)growingTextView:(HPGrowingTextView *)growingTextView didChangeHeight:(CGFloat)height
{
//    if ([_delegate respondsToSelector:@selector(commnetMessageSendPanel:inputPanelHeightDidChanged:)] == YES)
//        [_delegate commnetMessageSendPanel:self inputPanelHeightDidChanged:height];
}

- (void)growingTextViewDidChangeSelection:(HPGrowingTextView *)growingTextView
{

}

- (BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView
{
    if([_delegate respondsToSelector:@selector(commnetMessageSendPanelSendText:)])
       [_delegate commnetMessageSendPanelSendText:self];
    return YES;
}

@end
